package com.util;

import com.enums.ApiReturnCodeEnum;
import com.github.pagehelper.PageInfo;

import java.util.HashMap;
import java.util.Map;

/**
 * @author linyukun
 * @date 2022/7/6 18:54
 */
public class ResultUtil {
    public static String msg="执行成功";

    public static int code=0;
    /***
     * 返回自定义的code和msg
     */
    public static Result returnCodeAndMsg(int code,String msg){
        Result result=new Result();
        result.setCode(code);
        result.setMsg(msg);
        return result;
    }

    /**
     * 统一返回成功 带返回提示
     * 返回result成功提示
     */
    public static  Result returnResult(String result){
        Result dto=new Result();
        dto.setMsg(msg);
        dto.setCode(code);
        Map<String,String> map = new HashMap<>();
        map.put("resutlt",result);
        dto.setData(map);
        return dto;
    }


    /***
     * 统一返回成功的DTO 带数据 没有消息
     * 返回对象
     */
    public static Result returnDataSuccess(Object data){
        Result dto=new Result();
        dto.setMsg(msg);
        dto.setCode(code);
        dto.setData(data);
        return  dto;
    }
    /***
     * 统一返回分页的DTO 带数据 没有消息
     * 返回分页
     */
    public static Result returnPage(PageInfo pageInfo){
        Result dto=new Result();
        dto.setMsg(msg);
        dto.setCode(code);
        /*Map<String, List> map = new HashMap<>();
        map.put("resutlt",pageInfo.getList());*/
        PagedResult pagedResult = new PagedResult();
        pagedResult.setList(pageInfo.getList());
        pagedResult.setPageNum(pageInfo.getPageNum());
        pagedResult.setPageSize(pageInfo.getPageSize());
        pagedResult.setTotal(pageInfo.getTotal());
        pagedResult.setPages(pageInfo.getPages());
        dto.setData(pagedResult);
        return  dto;
    }

    /**
     *统一返回失败 带错误码
     */
    public static Result returnFail(ApiReturnCodeEnum apiReturnCodeEnum){
        Result dto=new Result();
        dto.setMsg(apiReturnCodeEnum.getDescription());
        dto.setCode(apiReturnCodeEnum.getCode());
        return  dto;
    }

    /**
     *统一返回失败 带错误码
     */
    public static Result returnFailMsg(String msg){
        Result dto = new Result();
        dto.setMsg(msg);
        dto.setCode(500);
        return  dto;
    }



}
